<template>
  <div class="tab-bar-item" @click="onClick">
    <slot v-if="!isActive" name="item-icon"></slot>
    <slot v-else name="item-icon-active"></slot>
    <div :class="{active: isActive}" :style="{color: currentColor}"><slot name="item-text"></slot></div>
    <!--class必须绑定到div上，不可绑在slot上，因slot渲染时会被替换导致class失效-->
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.path) !== -1;
    },
    currentColor() {
      return this.isActive ? this.activeColor : "black"
    }
  },
  props: {
    path: String,
    activeColor: {
      type: String,
      default: "deeppink",
    }
  },
  methods: {
    onClick() {
      this.$router.replace(this.path);
    }
  }
}
</script>

<style scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
}

.tab-bar-item img{
  margin-top: 3px;
  height: 24px;
}

.tab-bar-item .active{
  color: deeppink;
}
</style>
